<template>
  <ToolbarItemContainer
    :iconFile="redPacket"
    title="红包"
    :needDialog="false"
    iconWidth="30px"
    iconHeight="32px"
    @onIconClick="onIconClick"
  >
<!--    <div :class="['video-upload', !isPC && 'video-upload-h5']">
      <input
        title="视频"
        type="file"
        data-type="video"
        accept="video/*"
        @change="sendVideoInWeb"
        ref="inputRef"
      />
    </div> -->
  </ToolbarItemContainer>
</template>
<script lang="ts" setup>
import {
  TUIChatService,
  TUIStore,
  StoreName,
  IConversationModel,
  SendMessageParams,
} from "@tencentcloud/chat-uikit-engine";
import { TUIGlobal } from "@tencentcloud/universal-api";
import { ref } from "../../../../adapter-vue";
import { isPC, isWeChat, isUniFrameWork } from "../../../../utils/env";
import ToolbarItemContainer from "../toolbar-item-container/index.vue";
import redPacket from "../../../../assets/icon/red-packet.png";
import { Toast, TOAST_TYPE } from "../../../common/Toast";
import { isEnabledMessageReadReceiptGlobal } from "../../utils/utils";

const props = defineProps({


});

const emits = defineEmits(["close"]);

const inputRef = ref();
const currentConversation = ref<IConversationModel>();

TUIStore.watch(StoreName.CONV, {
  currentConversation: (conversation: IConversationModel) => {
    currentConversation.value = conversation;
  },
});



const onIconClick = () => {
	console.log(currentConversation.value,"currentConversation.value")
	let groupId=currentConversation.value?.groupProfile?.groupID||''
	let memberCount=currentConversation.value?.groupProfile?.memberCount||''
	let userId=currentConversation.value?.userProfile?.userID||''
	//跳转发红包
	TUIGlobal?.navigateTo({
		url:'/pages/TUIKit/components/TUIChat/redPacket/index?groupId='+groupId+'&userId='+userId+'&groupCount='+memberCount
	})
  // uniapp环境发送视频
  // if (isUniFrameWork) {
  //   // 增加 TUIGlobal.chooseMedia 条件限制，防御 uni 打包其他平台小程序时由于打包问题导致 isWeChat 为 true 出现运行时报错
  //   if (isWeChat && TUIGlobal?.chooseMedia) {
  //     // 微信小程序从基础库 2.21.0 开始， wx.chooseVideo 停止维护，请使用 uni.chooseMedia 代替
  //     TUIGlobal?.chooseMedia({
  //       mediaType: ["video"], // 视频
  //       count: 1,
  //       sourceType: [props.videoSourceType], // album 从相册选视频，camera 使用相机拍摄
  //       maxDuration: 60, // 设置最长时间60s
  //       success: function (res: any) {
  //         sendVideoMessage(res);
  //       },
  //     });
  //   } else {
  //     // uniapp h5/app 发送图片
  //     TUIGlobal?.chooseVideo({
  //       count: 1,
  //       sourceType: [props.videoSourceType], // 从相册选择或使用相机拍摄
  //       success: function (res: any) {
  //         sendVideoMessage(res);
  //       },
  //     });
  //   }
  // } else {
  //   inputRef?.value?.click && inputRef?.value?.click();
  // }
};

const sendVideoInWeb = (e: any) => {
  if (e?.target?.files?.length <= 0) {
    return;
  }
  sendVideoMessage(e?.target);
  e.target.value = "";
};

const sendVideoMessage = (file: any) => {
  if (!file) {
    return;
  }
  const options = {
    to:
      currentConversation?.value?.groupProfile?.groupID ||
      currentConversation?.value?.userProfile?.userID,
    conversationType: currentConversation?.value?.type,
    payload: {
      file,
    },
    needReadReceipt: isEnabledMessageReadReceiptGlobal(),
  } as SendMessageParams;
  TUIChatService.sendVideoMessage(options).catch((error : { message : string; code : number }) => {
			if (error.code === 20009) {
				Toast({
					message: '对方已不是你的好友',
					type: TOAST_TYPE.ERROR,
				})
			} else {
				Toast({
					message: error.message as string,
					type: TOAST_TYPE.ERROR,
				});
			}
		});
};
</script>
<style lang="scss" scoped>
@import url(../../../../assets/styles/common.scss);
</style>
